#include "stdafx.h"
#include "InputClass.h"


CInputClass::CInputClass(void) : m_pDirectInput(nullptr), m_pKeyboard(nullptr), m_pMouse(nullptr), m_nScreenWidth(0), m_nScreenHeight(0), m_nMouseXPos(0), m_nMouseYPos(0)
{
	
}


CInputClass::~CInputClass(void)
{
}

bool CInputClass::Initialize(HINSTANCE hInstance, HWND hWnd, int nScreenWidth, int nScreenHeight)
{
	HRESULT hResult;

	m_nScreenWidth = nScreenWidth;
	m_nScreenHeight = nScreenHeight;
	m_nMouseXPos = 0;
	m_nMouseYPos = 0;

	hResult = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDirectInput, NULL);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to create direct input 8");
		return false;
	}

	hResult = m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to create the keyboard direct input device");
		return false;
	}

	hResult = m_pKeyboard->SetDataFormat(&c_dfDIKeyboard);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to set keyboard data format");
		return false;
	}

	hResult = m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to set keyboard coop level");
		return false;
	}

	hResult = m_pKeyboard->Acquire();
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to acquire keyboard");
		return false;
	}

	hResult = m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to create mouse direct input device");
		return false;
	}

	hResult = m_pMouse->SetDataFormat(&c_dfDIMouse);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to set mouse data format");
		return false;
	}

	hResult = m_pMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to set mouse coop level");
		return false;
	}

	hResult = m_pMouse->Acquire();
	if(FAILED(hResult))
	{
		DEBUG_OUTPUT("Failed to acquire mouse");
		return false;
	}

	return true;
}

void CInputClass::ShutDown()
{
	if(m_pMouse)
	{
		m_pMouse->Unacquire();
		m_pMouse->Release();
		m_pMouse = nullptr;
	}

	if(m_pKeyboard)
	{
		m_pKeyboard->Unacquire();
		m_pKeyboard->Release();
		m_pKeyboard = nullptr;
	}

	if(m_pDirectInput)
	{
		m_pDirectInput->Release();
		m_pDirectInput = nullptr;
	}
}

bool CInputClass::Frame()
{
	bool bResult = false;

	bResult = ReadKeyboard();
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to read keyboard");
		return false;
	}

	bResult = ReadMouse();
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to read mouse");
		return false;
	}

	ProcessInput();

	return true;

}

bool CInputClass::IsEscapePressed()
{
	if(m_cKeyboardState[DIK_ESCAPE] & 0x80)
	{
		return true;
	}

	return false;
}

void CInputClass::GetMouseLocation(int& nXPos, int& nYPos)
{
	nXPos = m_nMouseXPos;
	nYPos = m_nMouseYPos;
}

bool CInputClass::ReadKeyboard()
{
	HRESULT hResult;

	hResult = m_pKeyboard->GetDeviceState(sizeof(m_cKeyboardState), (LPVOID)&m_cKeyboardState);
	if(FAILED(hResult))
	{
		if((hResult == DIERR_INPUTLOST) || (hResult == DIERR_NOTACQUIRED))
		{
			m_pKeyboard->Acquire();
		}
		else
		{
			DEBUG_OUTPUT("Failed to get keyboard device state");
			return false;
		}
	}

	return true;
}

bool CInputClass::ReadMouse()
{
	HRESULT hResult;

	hResult = m_pMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_tMouseState);
	if(FAILED(hResult))
	{
		if((hResult == DIERR_INPUTLOST) || (hResult == DIERR_NOTACQUIRED))
		{
			m_pMouse->Acquire();
		}
		else
		{
			DEBUG_OUTPUT("Failed to get mouse device state");
			return false;
		}
	}

	return true;
}

void CInputClass::ProcessInput()
{
	m_nMouseXPos += m_tMouseState.lX;
	m_nMouseYPos += m_tMouseState.lY;

	if(m_nMouseXPos < 0)
	{
		m_nMouseXPos = 0;
	}

	if(m_nMouseYPos < 0)
	{
		m_nMouseYPos = 0;
	}

	if(m_nMouseXPos > m_nScreenWidth)
	{
		m_nMouseXPos = m_nScreenWidth;
	}

	if(m_nMouseYPos > m_nScreenHeight)
	{
		m_nMouseYPos = m_nScreenHeight;
	}
}

bool CInputClass::IsLeftArrowPressed()
{
	if(m_cKeyboardState[DIK_LEFT] & 0x80)
	{
		return true;
	}

	return false;
}

bool CInputClass::IsRightArrowPressed()
{
	if(m_cKeyboardState[DIK_RIGHT] & 0x80)
	{
		return true;
	}

	return false;
}